<?php
// +----------------------------------------------------------------------
// | 初始执行文件
// +----------------------------------------------------------------------

use think\{Session, Cookie, Route, Hook};

// 检查sesstion目录
!is_dir(RUNTIME_PATH . 'sess') && @mkdir(RUNTIME_PATH . 'sess');

// 跨域
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:*');
header('Access-Control-Allow-Headers:*');
//header('Access-Control-Allow-Credentials:true');

defineDoman();
defineTime();
initSession();

initEstate();

// 域名
function defineDoman()
{
    if (isset($_SERVER['REQUEST_SCHEME'])  &&  isset($_SERVER['SERVER_NAME'])) {
        define('BASE_URL', $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST']);
    }
    if (isset($_SERVER['HTTP_HOST'])) {
        define('DOMAIN', $_SERVER['HTTP_HOST']);
    }
    if (isset($_SERVER['SERVER_NAME'])) {
        define('SERVER_NAME', $_SERVER['SERVER_NAME']);
    }
    if (isset($_REQUEST['s'])) {
        define('URL_AFTER', ltrim($_REQUEST['s'], "//"));
    }
}

// 时间
function defineTime()
{
    $now = date("Y-m-d H:i:s", time());
    define('NOW_DATE_TIME', $now);
    define('NOW_MICROTIME', date("YmdHis", time()) . substr(microtime(), 2, 6));
    define('NOW_DATE', substr($now, 0, 10));
    define('NOW_TIME', substr($now, 11));
    define('NOW_WEEK', array("日","一","二","三","四","五","六")[date("w")]);
}

// 预定义session
function initSession()
{
    /*if (Session::get('addons') === null) {
        Session::set('addons', array());
        // 每个插件的session
        foreach (get_addon_list() as $key=>$value) {
            Session::set('addons.' . $key, array());
        }
    }*/
    if (empty(Session::get(''))) {
        // 每个插件的session
        foreach (get_addon_list() as $key=>$value) {
            Session::set($key, array());
        }
    }
}

// 用户数据
function definedCustomer()
{
    $isLogin  = Session::has('customerData') ? true : false;
    if ($isLogin===false){
        \app\admin\model\Customer::loginByCookie();
        $isLogin  = Session::has('customerData') ? true : false;
    }

    $customer = $isLogin ? Session::get('customer') : null;
    $userData = $isLogin ? Session::get('customerData') : null;
    $GLOBALS['CUSTOMER'] = $customer;
    $GLOBALS['USER']     = $userData;
    define('IS_LOGIN', $isLogin);
}


// 初始化项目
function initEstate()
{
    Hook::add('menu_init', function (&$ruleList) {
        foreach ($ruleList as $key => &$rule)
{
            switch ($rule['name'])
            {
                case 'test':
                    addBadge($ruleList, $rule, 'Test', [], 'red');
                    break;
                case 'member/lists':
                    //addBadge($ruleList, $rule, 'member\Lists', [], 'green');
                    break;
                default:
                    break;
            }
        }
    });
}
